package com.java.test.collection.map;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Here is an extreme example in JAVA where I create a hash function that
 * puts all the data in the same bucket then I add 2 million elements.
 */
public class SkewedHashMap {

    public static void main(String[] args) {

        class MyKey {
            Integer i;
            public MyKey(Integer i){
                this.i =i;
            }

            /*@Override
            public int hashCode() {
                return 1;
            }*/
            /*@Override
            public int hashCode() {
                int key = 2097152-1;
                return key+2097152*i;
            }*/

            @Override
            public int hashCode() {
                return i;
            }

            @Override
            public boolean equals(Object obj) {
                if (obj instanceof MyKey) {
                    return i.equals(((MyKey) obj).i);
                } else
                    return false;
            }

        }
        Date begin = new Date();
        Map<MyKey,String> myMap= new HashMap<>(2_500_000,1);
        for (int i=0;i<2_000_0;i++){
            myMap.put( new MyKey(i), "test "+i);
        }

        Date end = new Date();
        System.out.println("Duration (ms) "+ (end.getTime()-begin.getTime()));
    }
}
